/* eslint-disable */
import Vue from 'vue'
import Router from 'vue-router'
const INDEX = () =>
    import('./views/Index.vue')
const LOGIN = () =>
    import('./views/login/Login.vue')
const NOACCESS = () =>
    import('./views/noAccess/noAccess.vue')
const PERMISSION = () =>
    import('./views/permissionPage/noPermission.vue')

// 内页功能模块
const snapShots = () => import('@/components/ganwei-app-snapshots/src/snapshots.vue')


// 四个基础模块
const EQUIPLIST = () => import('@/components/ganwei-app-equipList/src/equipListsIot.vue')
const HOME = () => import('@/components/ganwei-app-home/src/index.vue')
const EVENTQUERY = () => import('@/components/ganwei-app-event-querys/src/eqEvent')
const SETPAGE = () => import('@/components/ganwei-app-setPage/src/setPage')


// 扫码结果
const scanResults = () => import('@/components/ganwei-app-scanResults/src/scanResults.vue')

const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)
const appRouter = new Router({
    mode: 'hash',
    base: process.env.BASE_URL,
    linkActiveClass: 'active',
    routes: [
    {
        path: '/',
        redirect: '/Login'
    },
    {
        path: '/Login',
        name: 'Login',
        component: LOGIN
    },
    {
        path: '/index',
        name: 'INDEX',
        component: INDEX,
        children: [
            {
                path: 'noAccess',
                name: 'noAccess',
                component: NOACCESS
            },
            {
                path: 'noPermission',
                name: 'noPermission',
                component: PERMISSION
            },
            {
                path: 'equipList',
                name: 'EQUIPLIST',
                component: EQUIPLIST
            },
            {
                path: 'home',
                name: 'HOME',
                component: HOME
            },
            {
                path: 'eventQuery',
                name: 'EVENTQUERY',
                component: EVENTQUERY
            },
            {
                path: 'setpage',
                name: 'SETPAGE',
                component: SETPAGE
            },
            {
                path: 'snapShots/:type',
                name: 'SNAPSHOTS',
                component: snapShots
            },
            {
                path: "scanResults",
                name: "scanResults",
                component: scanResults
            },
        ]
    }, ]
})

appRouter.beforeEach((to, from, next) => {
    let toPath = to.path;
    let fromPath = from.path;
    // console.log(fromPath);
    // console.log(toPath);
    let name = to.query.title || '';
    // 跳转目标不包含 / index / snapShots（ 实际地址是携带参数 / index / snapShots / XX）
    let toUrl = toPath.indexOf('/index/snapShots') == -1 ? toPath : "/index/snapShots";
    //  菜单请求成功&&菜单不含有跳转目标&&跳转目标不是无权限提示页面&&跳转页面不是无权限显示页面&&跳转页面不是登录页&& 跳转页面不是首页
    if (sessionStorage.menu && JSON.stringify(sessionStorage.menu).indexOf(toUrl) == -1 && toUrl.indexOf("/index/noAccess") == -1 && toUrl.indexOf("/index/noPermission") == -1 && toUrl.indexOf("/Login") == -1 && toUrl.indexOf("/index/home") == -1) {
        next('/index/noPermission?title=' + name);
    }
    else {
        next();
    }
})
appRouter.afterEach((to, from, next) => {})
export default appRouter